package com.example.fairplay.newfpregistration.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.fairplay.newfpregistration.R;
import com.example.fairplay.newfpregistration.camera.CameraInterface;
import com.example.fairplay.newfpregistration.camera.FacePicInterface;
import com.example.fairplay.newfpregistration.camera.FragmentCamera;
import com.example.fairplay.newfpregistration.util.StatusBarCompat;
import com.example.mylibrary.util.ToastUtils;

import butterknife.Bind;
import butterknife.ButterKnife;

public class FaceToManyActivity extends FragmentActivity implements FacePicInterface {

	private FragmentCamera fragmentCamera = new FragmentCamera();
	private static final int MAX_VERIFY_COUNT = 5;
	private FragmentManager manager = getSupportFragmentManager();
	@Bind(R.id.text_title)
	TextView tvTitle;
	@Bind(R.id.button_backward)
	Button btnBack;

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 0:
					ToastUtils.Toast(context, "比对成功--->" + studentCode);
					break;
				case 1:
					ToastUtils.Toast(context, "比对失败");
					break;

				default:
					break;
			}
		}
	};
	private String studentCode;
	private Context context;
	public static FaceToManyActivity activity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_face_to_many);
		StatusBarCompat.compat(this, Color.rgb(7, 136, 242));
		ButterKnife.bind(this);
		context = this;
		activity = this;

		tvTitle.setText("人脸比对");

		btnBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		startFaceFragment();
	}

	private void startFaceFragment() {
		fragmentCamera = new FragmentCamera();
		CameraInterface.setVerifyCount(MAX_VERIFY_COUNT);
		FragmentTransaction transaction = manager.beginTransaction();
		transaction.add(R.id.activity_face_to_many, fragmentCamera);
		transaction.addToBackStack(null);
		transaction.commit();
	}


	@Override
	public void fromPic(int requestCode, int resultCode, Intent data) {
		Log.i("request--->", requestCode + "->" + resultCode + "->");
		if (requestCode == 222 && resultCode == 1) {
			float score1 = data.getFloatExtra("score1", -3);
			studentCode = data.getStringExtra("student");
			if (score1 == -3 || studentCode == null || studentCode.equals("")) {
				Log.i("score1", "---->" + score1);
				mHandler.sendEmptyMessage(1);
			}
			Log.i("score1", "--->" + score1 + "-->" + studentCode);
			if (score1 != -1 && score1 != 0) {
				mHandler.sendEmptyMessage(0);
			}
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					removeFragment();
					startFaceFragment();
				}
			}, 3000);
		} else if (requestCode == 222 && resultCode == 0) {
			finish();
		}
	}

	public FragmentCamera getFragment() {
		return fragmentCamera;
	}

	public void removeFragment() {
		if (fragmentCamera != null) {
			manager.beginTransaction().remove(fragmentCamera).commit();
		}
	}

	private void getScore(byte[] b1, byte[] b2) {
		float score = MainMenu.getAraFaceAuthLib().verify(b1, b2);
		Log.i("---score---", "getScore: " + score);
	}
}
